/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.middleware.quartz.job.jgupload;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.report.impl.TjJgReportService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 天津上报脚本
 *
 * @author duanlikao
 * @created 2021/5/13
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class TjUploadJobService  implements Job {

    private static final Logger LOGGER = LoggerFactory.getLogger(TjUploadJobService.class);

    public SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IHyShippingNoteService shippingNoteService;

    @Autowired
    private IHyCompanyConfigService companyConfigService;

    @Resource
    private TjJgReportService tjJgReportService;


    //@Scheduled(cron = "0 0 5 * * ?")//每天的5:00:00执行
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getTjReportIzUse()){
            LOGGER.info("天津上报服务暂未开启,请联系管理。");
            return;
        }

        List<HyCompanyConfig> companyConfigs = companyConfigService.list(new QueryWrapper<HyCompanyConfig>()
                .eq("report_platform_type", ReportPlatformEnum.REPORT_PLATFORM_TJ.getCode())
                .eq("auto_report",1)
        );
        if(CollectionUtils.isEmpty(companyConfigs)) {
            LOGGER.info("TjUploadJobService autoUploadTjShippingNot: 没有分公司配置自动上报 !" );
            return;
        }
        Long start = System.currentTimeMillis();
        LOGGER.info("TjUploadJobService autoUploadTjShippingNot:执行运单上报数据开始" + now.format(new Date()));

        for (HyCompanyConfig companyConfig : companyConfigs) {
            QueryWrapper<HyShippingNote> queryWrapper = new QueryWrapper();

            queryWrapper.eq("shipping_pay_status","6");
            //天津分公司
            queryWrapper.eq("company_id",companyConfig.getCompanyId());
            queryWrapper.eq("upload_status","0");

            queryWrapper.orderByDesc("pay_dt");
            List<HyShippingNote> shippingNotes = shippingNoteService.list( queryWrapper);

            if(CollectionUtils.isEmpty(shippingNotes)) {
                LOGGER.info("TjUploadJobService autoUploadTjShippingNot: no data handling !" );
                continue;
            }
            LOGGER.info("-----------------------------执行定时任务了--------------------------"+shippingNotes.size());
            for(HyShippingNote hyShippingNote:shippingNotes){
                LOGGER.info("-----------------------------执行定时任务了--------------------------");
                try {
                    TimeUnit.SECONDS.sleep(2);
                    tjJgReportService.report(hyShippingNote);
                }catch (Exception e){
                    LOGGER.info("TjUploadJobService autoUploadTjShippingNot:Exception : {}" ,e.getMessage());
                    continue;
                }
            }

        }


        LOGGER.info("TjUploadJobService autoUploadTjShippingNot:执行运单上报数据结束,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");

    }

}
